﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;

namespace KhoBanHang
{
    public partial class HoaDonBanHang : Form
    {
        
        List<Business.HoaDonBan> danhSachHDBan;
        List<Business.HangHoa> danhSachHangHoa;
        List<Business.DonDatKH> danhSachDonDatHang;

        Business.HoaDonBan HoaDonBan;
        Business.HangHoa hangHoa;
        

        
        public HoaDonBanHang()
        {
            InitializeComponent();
        }

        private void HoaDonBan_Load(object sender, EventArgs e)
        {
            LoadDanhSachHang();
            LoadDanhSachDonDatHang();
            LoadDanhsachHoaDon(true);

            dtpickerDenNgay.CustomFormat = "dd/MM/yyyy";
            dtpickerDenNgaydshd.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayTaoHD.CustomFormat = "dd/MM/yyyy";
            dtpickerTuNgay.CustomFormat = "dd/MM/yyyy";
            dtpickerTuNgaydshd.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayTaoHD2.CustomFormat = "dd/MM/yyyy";

            if (@Session.quyen == "3") 
            {
                btnThemHD.Enabled = false;
               // btnXoaHD.Enabled = false;
                btnThemCTHD.Enabled = false;
                btnLuuCTHD.Enabled = false;
                btnXoaCTHD.Enabled = false;
                btnLapPhieuXK.Enabled = false;
                btnLuuHD.Enabled = false;
            }

                     
            dtpickerNgayTaoHD2.CustomFormat = "dd/MM/yyyy";
            HoaDonBan = new Business.HoaDonBan();
            txtThueSuat2.Text = HoaDonBan.ThueSuatHienThi.ToString();

            txtMaHDcthd.Text = "";
            cbxDonDatHang.SelectedItem = null;
            txtMaNVcthd.Text = "NV" + @Session.maNV;

            XoaTrangChiTietHDBan();
            gridviewChiTietHDBan.DataSource = null;
            txtTongTienHang.Text = "";
            txtTienCanTT.Text = "";

            dtpickerNgayTaoHD2.Enabled = true;
            cbxTenHang2.Enabled = true;
            txtSLBan.Enabled = true;
            txtTiLeCK.Enabled = true;
            
        }

        private void LoadDanhSachHang()
        {
            danhSachHangHoa = Business.HangHoa.LayDanhSach();

            cbxTenHang2.DataSource = danhSachHangHoa;
            cbxTenHang2.DisplayMember = "TenHang";
            cbxTenHang2.ValueMember = "MaHangHienThi";

            cbxTenHang2.SelectedItem = null;
        }

        private void LoadDanhSachDonDatHang()
        {
            danhSachDonDatHang = Business.DonDatKH.LayDanhSach();

            cbxDonDatHang.DataSource = danhSachDonDatHang;
            cbxDonDatHang.DisplayMember = "MaDDHHienThi";
            cbxDonDatHang.ValueMember = "MaDDH";

            cbxDonDatHang.SelectedItem = null;
        }

        private void LoadDanhsachHoaDon(bool lanDau)
        {
            if (lanDau)
            {
                danhSachHDBan = Business.HoaDonBan.LayDanhSach();
            }
            else
            {
                if (rbtnNgaydshd.Checked && dtpickerTuNgay.Value != null)
                    danhSachHDBan = Business.HoaDonBan.LayDanhSach_NgayMua(dtpickerTuNgay.Value, dtpickerDenNgay.Value);
                else if (rbtnMaDonDat.Checked && txtMaDonDat.Text.Contains("DDKH"))
                    danhSachHDBan = Business.HoaDonBan.LayDanhSach_MaDonDat(txtMaDonDat.Text);
                else if (rbtnMaHDdshd.Checked && txtMaHD.Text.Contains("HDB"))
                    danhSachHDBan = Business.HoaDonBan.LayDanhSach_MaHoaDonBan(txtMaHD.Text);
                else
                    danhSachHDBan = Business.HoaDonBan.LayDanhSach();
            }

            gridviewDSHD.DataSource = danhSachHDBan;
            gridviewDSHD.Columns["MaHDB"].Visible = false;
            gridviewDSHD.Columns["ThueSuat"].Visible = false;
            gridviewDSHD.Columns["MaNV"].Visible = false;
            gridviewDSHD.Columns["MaDDH"].Visible = false;
            gridviewDSHD.Columns["NgayXuatHD"].DefaultCellStyle.Format = "dd/MM/yyyy";
            gridviewDSHD.Columns["TienTruocThue"].DefaultCellStyle.Format = "##,###,###";
            gridviewDSHD.Columns["TienSauThue"].DefaultCellStyle.Format = "##,###,###";

            gridviewDSHD.Columns["MaHDBHienThi"].HeaderText = "Mã hóa đơn";
            gridviewDSHD.Columns["MaNVHienThi"].HeaderText = "Nhân viên lập";
            gridviewDSHD.Columns["MaDDHHienThi"].HeaderText = "Đơn đặt hàng";
            gridviewDSHD.Columns["NgayXuatHD"].HeaderText = "Ngày xuất hóa đơn";
            gridviewDSHD.Columns["TienTruocThue"].HeaderText = "Tiền trước thuế";
            gridviewDSHD.Columns["TienSauThue"].HeaderText = "Tiền sau thuế";
            gridviewDSHD.Columns["ThueSuatHienThi"].HeaderText = "Thuế suất (%)";
        }

        public void LoadDanhSachChiTietHDBan()
        {
            BindingSource source = new BindingSource();
            source.DataSource = HoaDonBan.DanhSachChiTietHDBan;

            gridviewChiTietHDBan.DataSource = source;
            gridviewChiTietHDBan.RefreshEdit();
            gridviewChiTietHDBan.Refresh();

            gridviewChiTietHDBan.Columns["MaCTHDB"].Visible = false;
            gridviewChiTietHDBan.Columns["MaHang"].Visible = false;
            gridviewChiTietHDBan.Columns["MaHDB"].Visible = false;
            gridviewChiTietHDBan.Columns["ChietKhau"].Visible = false;

            gridviewChiTietHDBan.Columns["MaCTHDBHienThi"].HeaderText = "Mã chi tiết hóa đơn";
            gridviewChiTietHDBan.Columns["MaHangHienThi"].HeaderText = "Mã hàng";
            gridviewChiTietHDBan.Columns["TenHang"].HeaderText = "Tên hàng";
            gridviewChiTietHDBan.Columns["SoLuong"].HeaderText = "Số lượng";
            gridviewChiTietHDBan.Columns["DonGiaBan"].HeaderText = "Đơn giá bán";
            gridviewChiTietHDBan.Columns["ChietKhauHienThi"].HeaderText = "Chiết khấu (%)";

            gridviewChiTietHDBan.Columns["DonGiaBan"].DefaultCellStyle.Format = "##,###,###";

            HoaDonBan.TinhTongTienSauThue(double.Parse(txtThueSuat2.Text)).ToString();
            txtTongTienHang.Text = HoaDonBan.TienTruocThue.ToString();
            txtTienCanTT.Text = HoaDonBan.TienSauThue.ToString();
        }

        private void LoadThongTinHoaDonBan(int maHDB)
        {
            HoaDonBan = Business.HoaDonBan.LayHoaDonBan(maHDB);
            dtpickerNgayTaoHD2.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayTaoHD2.Text = HoaDonBan.NgayXuatHD.ToString();
            txtMaHDcthd.Text = HoaDonBan.MaHDBHienThi;

            txtMaNVcthd.Text = "NV" + HoaDonBan.MaNV.ToString();
            cbxDonDatHang.SelectedValue = HoaDonBan.MaDDH;
            txtThueSuat2.Text = HoaDonBan.ThueSuatHienThi.ToString();

            LoadDanhSachChiTietHDBan();
        }

        private void ChoPhepChinhSua(int maHDBan)
        {
            if (!Business.HoaDonBan.ChoPhepXoaChinhSua(maHDBan))
            {
                btnThemCTHD.Enabled = false;
                btnLuuCTHD.Enabled = false;
                btnXoaCTHD.Enabled = false;

                btnLuuHD.Enabled = false;
                btnLapPhieuXK.Enabled=false;
                dtpickerNgayTaoHD2.Enabled = false;
                cbxDonDatHang.Enabled = false;
                cbxTenHang2.Enabled = false;
                txtSLBan.ReadOnly = true;
                txtTiLeCK.ReadOnly = true;
                txtThueSuat2.ReadOnly = true;
            }
            else 
            {
                btnThemCTHD.Enabled = true;
                btnLuuCTHD.Enabled = true;
                btnXoaCTHD.Enabled = true;
                btnLuuHD.Enabled = true;
                btnLapPhieuXK.Enabled = true;
                dtpickerNgayTaoHD2.Enabled = true;
                cbxDonDatHang.Enabled = true;
                cbxTenHang2.Enabled = true;
                txtSLBan.ReadOnly = false;
                txtTiLeCK.ReadOnly = false;
                txtThueSuat2.ReadOnly = false;
            }
        }
        

       
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                LoadDanhsachHoaDon(false);
            }
        }

        private void cbxTenHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxTenHang2.SelectedItem != null && HoaDonBan != null)
            {
                hangHoa = danhSachHangHoa[cbxTenHang2.SelectedIndex];
                txtSLTon.Text = hangHoa.SoLuongTon.ToString();
                txtMaHang2.Text = hangHoa.MaHangHienThi.ToString();
                txtGiaBan.Text = hangHoa.GiaBan.ToString();
            }
        }

        private void btnLapPhieuXK_Click(object sender, EventArgs e)
        {
            if (HoaDonBan.MaHDB == 0) 
            {
                MessageBox.Show("Phải lưu hóa đơn bán trước khi lập phiếu xuất kho");
            }
            else
            {
                PhieuXuatKho phieuXuatKho = new PhieuXuatKho();
                phieuXuatKho.LayTheoHDB(HoaDonBan.MaHDB);
                phieuXuatKho.ShowDialog();
            }
        }
        

      
        private void btnTim_Click(object sender, EventArgs e)
        {
            LoadDanhsachHoaDon(false);
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            if (danhSachHDBan != null && danhSachHDBan.Count > 0)
            {
                tabControl1.SelectedTab = this.tabPage5;
                if (gridviewDSHD.SelectedRows.Count == 0)
                {
                    int maHDBan = danhSachHDBan[0].MaHDB;
                    ChoPhepChinhSua(maHDBan);
                    LoadThongTinHoaDonBan(maHDBan);
                }
                else
                {
                    int maHDBan = danhSachHDBan[gridviewDSHD.SelectedRows[0].Index].MaHDB;
                    ChoPhepChinhSua(maHDBan);
                    LoadThongTinHoaDonBan(maHDBan);
                }
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage5;
            dtpickerNgayTaoHD2.CustomFormat = "dd/MM/yyyy";
            HoaDonBan = new Business.HoaDonBan();
            txtThueSuat2.Text = HoaDonBan.ThueSuatHienThi.ToString();

            txtMaHDcthd.Text = "";
            cbxDonDatHang.SelectedItem = null;
            txtMaNVcthd.Text = "NV" + @Session.maNV;
            txtMaHDcthd.Enabled = true;
            cbxDonDatHang.Enabled = true;
            txtMaNVcthd.Enabled = true;

            XoaTrangChiTietHDBan();

            gridviewChiTietHDBan.DataSource = null;
            txtTongTienHang.Text = "";
            txtTienCanTT.Text = "";

            dtpickerNgayTaoHD2.Enabled = true;
            dtpickerNgayTaoHD2.Value = DateTime.Now;
            btnLuuHD.Enabled = true;
            cbxTenHang2.Enabled = true;
            txtSLBan.ReadOnly = false;
            txtTiLeCK.ReadOnly = false;
            btnThemCTHD.Enabled = true;
            btnLuuCTHD.Enabled = true;
            btnXoaCTHD.Enabled = true;



        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (HoaDonBan.MaHDB == 0)
            {
                CapNhatThongTinChung();
                int maHDBan = Business.HoaDonBan.ThemMoi(HoaDonBan);

                LoadThongTinHoaDonBan(maHDBan);
                MessageBox.Show("Thêm mới hóa đơn bán thành công");
            }
            else
            {
                CapNhatThongTinChung();
                Business.HoaDonBan.CapNhat(HoaDonBan);

                LoadThongTinHoaDonBan(HoaDonBan.MaHDB);
                MessageBox.Show("Cập nhật hóa đơn bán thành công");
            }

            XoaTrangChiTietHDBan();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (danhSachHDBan != null && danhSachHDBan.Count > 0)
            {
                if (gridviewDSHD.SelectedRows.Count > 0)
                {
                    int maHDBan = danhSachHDBan[gridviewDSHD.SelectedRows[0].Index].MaHDB;
                    if (Business.HoaDonBan.ChoPhepXoaChinhSua(maHDBan))
                    {
                        Business.HoaDonBan.Xoa(maHDBan);
                        LoadDanhsachHoaDon(false);
                    }
                    else 
                    {
                        MessageBox.Show("Hóa đơn đã lập phiếu xuất kho. Không cho phép xóa");
                    }
                }
            }
        }

        private void CapNhatThongTinChung()
        {
            HoaDonBan.MaNV = Int16.Parse(txtMaNVcthd.Text.Substring(2));
            HoaDonBan.MaDDH = (int)cbxDonDatHang.SelectedValue;
            HoaDonBan.NgayXuatHD = dtpickerNgayTaoHD2.Value;
            HoaDonBan.TinhTongTienSauThue(double.Parse(txtThueSuat2.Text));
        }
        

        
        bool dangThemMoi = true;
        private void gridviewChiTietHoaDonBan_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            ChiTietHDBan chiTietHDBan = HoaDonBan.DanhSachChiTietHDBan[e.RowIndex];
            cbxTenHang2.SelectedValue = "HH" + chiTietHDBan.MaHang;
            txtSLBan.Text = chiTietHDBan.SoLuong.ToString();
            txtTiLeCK.Text = chiTietHDBan.ChietKhauHienThi.ToString();
            dangThemMoi = false;
        }

        private void XoaTrangChiTietHDBan()
        {
            cbxTenHang2.SelectedItem = null;
            txtMaHang2.Text = "";
            txtSLTon.Text = "";
            txtSLBan.Text = "";
            txtTiLeCK.Text = "0";
            txtGiaBan.Text = "";
            dangThemMoi = true;
        }

        private void btnThemCTDD_Click(object sender, EventArgs e)
        {
            XoaTrangChiTietHDBan();
        }

        private void btnLuuCTDD_Click(object sender, EventArgs e)
        {
            int maHDBan = HoaDonBan.MaHDB;
            int maHang = Int16.Parse(txtMaHang2.Text.Substring(2));
            string tenHang = ((HangHoa)cbxTenHang2.SelectedItem).TenHang;
            int soLuong = Int16.Parse(txtSLBan.Text);
            double giaBan = double.Parse(txtGiaBan.Text);
            double chietKhau = double.Parse(txtTiLeCK.Text) / 100;

            if (soLuong > hangHoa.SoLuongTon) 
            {
                MessageBox.Show("Số lượng bán phải nhỏ hơn số lượng tồn!");
                return;
            }
            ChiTietHDBan chiTietHDBan = new ChiTietHDBan(maHDBan, maHang, tenHang, soLuong, giaBan, chietKhau);
            if (dangThemMoi == true)
            {
                HoaDonBan.ThemMoiChiTietHDBan(maHDBan, maHang, tenHang, soLuong, giaBan, chietKhau);
            }
            else
            {
                if (gridviewChiTietHDBan.SelectedRows.Count > 0)
                {
                    int index = gridviewChiTietHDBan.SelectedRows[0].Index;
                    HoaDonBan.CapNhatChiTietDonDat(index, maHang, tenHang, soLuong, giaBan, chietKhau);
                }
            }

            LoadDanhSachChiTietHDBan();
            XoaTrangChiTietHDBan();
        }

        private void btnXoaCTDD_Click(object sender, EventArgs e)
        {
            int index = gridviewChiTietHDBan.SelectedRows[0].Index;
            HoaDonBan.XoaChiTietHDBan(index);
            LoadDanhSachChiTietHDBan();
        }
        

        private void dtpickerDenNgay_ValueChanged(object sender, EventArgs e)
        {

        }

        private void cbxDonDatHang_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}